package com.bingo.generate.comm.util;

import com.bingo.generate.comm.service.GenerateDbService;
import lombok.Setter;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ClassName
 * Description
 * Create by lc
 * Date 2021/12/15 11:50
 * php的id变成long的帮助类
 * 该类是线程不安全的
 */
public class PhpIdToLangUtil {

    @Setter
    private GenerateDbService dbService;

    private String pathStr;

    @Setter
    private List<String> allowChildList= Collections.emptyList();

    private Path dirPath;

    private List<Path> waitHandlePathList=new ArrayList<>();

    public void setPathStr(String pathStr) {
        pathStr=pathStr.replace("/","\\");
        this.pathStr = pathStr;
        dirPath= Paths.get(pathStr);
    }

    public void analysis(){
        try {
            analysis0();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void analysis0() throws Exception {
        Files.walkFileTree(dirPath, new FileVisitor<Path>() {
            // 在访问子目录前触发该方法
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("正在访问" + dir + "目录");
                return FileVisitResult.CONTINUE;
            }

            // 在访问文件时触发该方法
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                /*if (file.endsWith("FilesTest.java")) {
                    System.out.println("------已找到FilesTest.java,文件内容-----");
                    List<String> list = Files.readAllLines(file);
                    // 打印出文件的内容
                    System.out.println(list);
                    return FileVisitResult.TERMINATE;
                }*/
                String fileStr=file.toAbsolutePath().toString();
                String suffix =  fileStr.replace("/","\\").replace(pathStr,"");
                suffix=new StringBuilder(suffix).deleteCharAt(0).toString();
                if(!allowChildList.contains(suffix)){
                    //analysisOnePhp(file);
                    waitHandlePathList.add(file);
                }
                return FileVisitResult.CONTINUE;
            }

            // 在访问失败时触发该方法
            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                // 写一些具体的业务逻辑
                return FileVisitResult.TERMINATE;
            }

            // 在访问目录之后触发该方法
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                // 写一些具体的业务逻辑
                return FileVisitResult.CONTINUE;
            }
        });

        if(waitHandlePathList.size()>0){
            analysisOnePhp(waitHandlePathList.get(0));
        }
        for (Path path : waitHandlePathList) {
            System.out.println("待处理的文件路由: "+path);
        }

    }

    /**
     * 分析单个php文件
     */
    public void analysisOnePhp(Path path){
        System.out.println("待处理的文件路由: "+path);
    }

}
